<template>
  <div>
    <a-modal v-model="visible" :confirmLoading="loading" :maskClosable="false" @cancel="cancel" @ok="confirm">
      <div slot="title">{{form.id ? '编辑旧账管理' : '新增旧账管理' }}</div>
      <div>
        <a-form-model ref="form" :model="form" :rules="rules" :label-col="{ span: 6 }" :wrapper-col="{ span: 16 }">
          <a-form-model-item prop="number" label="编号">
            <a-input v-model="form.number" />
          </a-form-model-item>
          <a-form-model-item prop="client" label="客户">
            <a-select v-model="form.client" style="width: 100%" allowClear showSearch :filterOption="filterOption">
              <a-select-option v-for="item in clientsItems" :key="item.id" :value="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item prop="handler" label="经手人">
            <a-select v-model="form.handler" style="width: 100%" allowClear>
              <a-select-option v-for="item in handlerItems" :key="item.id" :value="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item prop="handle_time" label="处理时间">
            <a-date-picker v-model="form.handle_time" valueFormat="YYYY-MM-DD HH:mm:ss" style="width: 100%" />
          </a-form-model-item>
          <a-form-model-item prop="charge_item" label="项目">
            <a-select v-model="form.charge_item" style="width: 100%">
              <a-select-option v-for="item in chargeItems_new" :key="item.id" :value="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-model-item>

          <a-form-model-item prop="total_amount" label="应收金额">
            <a-input-number v-model="form.total_amount" style="width: 100%;"  @change="total_amountChange()"/>
          </a-form-model-item>
          <a-form-model-item prop="is_pay" label="是否已收款">
            <a-switch v-model="form.is_pay" checkedChildren="已收款" unCheckedChildren="未收款"/>  
          </a-form-model-item>
          <a-form-model-item prop="account" label="结算账户" v-if="form.is_pay">
            <a-select v-model="form.account" style="width: 100%">
              <a-select-option v-for="item in accountsItems" :key="item.id" :value="item.id">
                {{ item.name }}
              </a-select-option>
            </a-select>
          </a-form-model-item>
          <a-form-model-item prop="charge_amount" label="实收金额" v-if="form.is_pay">
            <a-input-number ref="chargeAmountInput" v-model="form.charge_amount" style="width: 100%;" />
          </a-form-model-item>
          <a-form-model-item prop="remark" label="备注">
            <a-input v-model="form.remark" allowClear />
          </a-form-model-item>
        </a-form-model>
      </div>
    </a-modal>
  </div>
</template>

<script>
  import { oldAccountUpdate,oldAccountCreate } from '@/api/finance'
  
  export default {
    name: 'FormModal',
    props: ['visible', 'form', 'clientsItems', 'suppliersItems', 'chargeItems', 'handlerItems', 'accountsItems'],
    model: { prop: 'visible', event: 'cancel' },
    data() {
      return {
        typeItems: [
          { id: 'income', name: '收入' },
          { id: 'expenditure', name: '支出' }
        ],
        rules: {
          number: [{ required: true, message: '请输入编号', trigger: 'change' }],
          type: [{ required: true, message: '请选择收支类型', trigger: 'change' }],
          client: [{ required: true, message: '请选择客户', trigger: 'change' }],
          account: [{ required: true, message: '请选择结算账户', trigger: 'change' }],
          total_amount: [
            { required: true, message: '请输入应收/付金额', trigger: 'change' },
            { pattern: new RegExp(/^\d{0,14}(?:\.\d{0,2})?$/), message: '应收/付金额格式不正确', trigger: 'change' }
          ],
          charge_amount: [
            { required: true, message: '请输入实收/付金额', trigger: 'change' },
            { pattern: new RegExp(/^\d{0,14}(?:\.\d{0,2})?$/), message: '实收/付金额格式不正确', trigger: 'change' }
          ],
          charge_item: [{ required: true, message: '请选择收支项目', trigger: 'change' }],
          handler: [{ required: true, message: '请选择经手人', trigger: 'change' }],
          handle_time: [{ required: true, message: '请选择处理时间', trigger: 'change' }],
        },
        loading: false,
        updating: false,
      };
    },
  watch: {
    'form.is_pay': {
      immediate: true,
      handler(newVal) {
        if (newVal && this.form.id) {
          this.$set(this.form,'charge_amount',this.form.charge_amount)       
        } else {
          this.$set(this.form,'charge_amount',0)   
          this.$set(this.form,'account',null)   
        }
      }
    },
    // 'form.total_amount': {
    //   immediate: true,
    //   handler(newVal) {
    //     if (this.form.is_pay) {
    //       this.$set(this.form,'charge_amount',newVal)          
    //     }
    //   }
    // },
  },
  computed: {
    chargeItems_new:{
      get() {
        return this.chargeItems.filter(i=>i.type=='income');
      },
    },
  },  
    methods: {
      updateChargeAmount() {
        if (this.form.is_pay) {
          this.form.charge_amount = parseFloat(this.form.charge_amount) || 0;
        } else {
          this.form.charge_amount = 0;
        }
      },
      // 将输入的内容与显示的内容进行匹配
      filterOption (value, option) {
        return option.componentOptions.children[0].text.indexOf(value) >= 0
      },
      total_amountChange(){
        // this.form.charge_amount=this.form.total_amount
      },
      confirm() {
        if(this.form.is_pay){
          if(this.form.charge_amount<=0){
            this.$message.warning('实收金额不能小于或等于0');
            return
          }
        }
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true;
            let func = this.form.id ? oldAccountUpdate : oldAccountCreate;
            func(this.form).then(data => {
              this.$message.success(this.form.id ? '修改成功' : '新增成功');
              this.$emit(this.form.id ? 'update' : 'create', data);
              this.cancel();
            }).finally(() => {
              this.loading = false;
            });
          }
        });
      },
      cancel() {
        this.$emit('cancel', false);
        this.$refs.form.resetFields();
      },
    },
  }
</script>

<style scoped>
</style>